package tasknow.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import tasknow.ui.ActiveTasksComposite;
import tasknow.ui.DisplayAndCommandTextComposite;

public class GraphicalUserInterface {

	// @author A0098740W

	protected Shell shell;

	private static boolean firstTimeOpening = true;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			GraphicalUserInterface window = new GraphicalUserInterface();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		shell.setSize(1140, 690);
		shell.setMinimumSize(1140, 690);
		center(shell);
		shell.setText("TaskNow");
		shell.setLayout(new GridLayout(2, false));

		ActiveTasksComposite activeTasksComposite = new ActiveTasksComposite(
				shell, SWT.NONE);
		GridData gd_activeTasksComposite = new GridData(SWT.FILL, SWT.TOP,
				false, false, 1, 1);
		gd_activeTasksComposite.widthHint = 288;
		gd_activeTasksComposite.heightHint = 620;
		activeTasksComposite.setLayoutData(gd_activeTasksComposite);

		DisplayAndCommandTextComposite displayAndCommandTextComposite = new DisplayAndCommandTextComposite(
				shell, SWT.NONE);
		GridData gd_displayAndCommandTextComposite = new GridData(SWT.FILL,
				SWT.TOP, false, false, 1, 1);
		gd_displayAndCommandTextComposite.heightHint = 620;
		gd_displayAndCommandTextComposite.widthHint = 820;
		displayAndCommandTextComposite
		.setLayoutData(gd_displayAndCommandTextComposite);

		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);

		MenuItem mntmHelp = new MenuItem(menu, SWT.NONE);
		mntmHelp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Shell shellForHelpWindow = new Shell();
				HelpWindowDisplay HelpWindowDisplay = new HelpWindowDisplay(shellForHelpWindow, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
			}
		});
		mntmHelp.setText("Help");

		setupInitialDisplay();
	}

	private void setupInitialDisplay() {
		DisplayAndCommandTextComposite.loadTasksToTaskNow();

		if (firstTimeOpening) {	
			DisplayAndCommandTextComposite.initialiseDisplay();
			firstTimeOpening = false;
		}

		DisplayAndCommandTextComposite.readyTaskNow();
	}

	protected void center(Shell shell) {
		Rectangle bds = shell.getDisplay().getBounds();
		Point p = shell.getSize();
		int nLeft = (bds.width - p.x) / 2;
		int nTop = (bds.height - p.y) / 2;

		shell.setBounds(nLeft, nTop, p.x, p.y);
	}

}
